{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 76,
   "metadata": {},
   "outputs": [],
   "source": [
    "base_config = {\"initial_amount\": 100000, \"transaction_cost_pct\":\n",
    "                       1e-4, \"tech_indicator_list\": [\"zopen\", \"zhigh\", \"zlow\", \"zadjcp\", \"zclose\", \"zd_5\", \"zd_10\", \"zd_15\", \"zd_20\", \"zd_25\", \"zd_30\"]}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 77,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd\n",
    "\n",
    "train=pd.read_csv(\"/home/sunshuo/qml/TradeMaster_reframe/experiment_result/data/train.csv\")\n",
    "valid=pd.read_csv(\"/home/sunshuo/qml/TradeMaster_reframe/experiment_result/data/valid.csv\")\n",
    "test=pd.read_csv(\"/home/sunshuo/qml/TradeMaster_reframe/experiment_result/data/test.csv\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 78,
   "metadata": {},
   "outputs": [],
   "source": [
    "train_config = {\"df_dict\": \"/home/sunshuo/qml/TradeMaster_reframe/experiment_result/data/train.csv\"}\n",
    "train_config.update(base_config)\n",
    "valid_config = {\"df_dict\": \"/home/sunshuo/qml/TradeMaster_reframe/experiment_result/data/valid.csv\"}\n",
    "valid_config.update(base_config)\n",
    "test_config = {\"df_dict\": \"/home/sunshuo/qml/TradeMaster_reframe/experiment_result/data/test.csv\"}\n",
    "test_config.update(base_config)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {},
   "outputs": [],
   "source": [
    "import yaml\n",
    "yamlpath=\"/home/sunshuo/qml/TradeMaster_reframe/input_config/env/portfolio/train.yml\"\n",
    "with open(yamlpath, \"w\", encoding=\"utf-8\") as f:\n",
    "    yaml.dump(train_config, f)\n",
    "yamlpath=\"/home/sunshuo/qml/TradeMaster_reframe/input_config/env/portfolio/valid.yml\"\n",
    "with open(yamlpath, \"w\", encoding=\"utf-8\") as f:\n",
    "    yaml.dump(valid_config, f)\n",
    "yamlpath=\"/home/sunshuo/qml/TradeMaster_reframe/input_config/env/portfolio/test.yml\"\n",
    "with open(yamlpath, \"w\", encoding=\"utf-8\") as f:\n",
    "    yaml.dump(test_config, f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'_disable_action_flattening': False,\n",
       " '_disable_execution_plan_api': False,\n",
       " '_disable_preprocessor_api': False,\n",
       " '_fake_gpus': False,\n",
       " '_tf_policy_handles_more_than_one_loss': False,\n",
       " 'action_space': None,\n",
       " 'actions_in_input_normalized': False,\n",
       " 'actor_hidden_activation': 'relu',\n",
       " 'actor_hiddens': [400, 300],\n",
       " 'actor_lr': 0.001,\n",
       " 'always_attach_evaluation_results': False,\n",
       " 'batch_mode': 'truncate_episodes',\n",
       " 'buffer_size': -1,\n",
       " 'clip_actions': False,\n",
       " 'clip_rewards': None,\n",
       " 'collect_metrics_timeout': -1,\n",
       " 'compress_observations': False,\n",
       " 'create_env_on_driver': False,\n",
       " 'critic_hidden_activation': 'relu',\n",
       " 'critic_hiddens': [400, 300],\n",
       " 'critic_lr': 0.001,\n",
       " 'custom_eval_function': None,\n",
       " 'custom_resources_per_worker': {},\n",
       " 'disable_env_checking': False,\n",
       " 'eager_max_retraces': 20,\n",
       " 'eager_tracing': False,\n",
       " 'env': None,\n",
       " 'env_config': {},\n",
       " 'env_task_fn': None,\n",
       " 'evaluation_config': {'explore': False},\n",
       " 'evaluation_duration': 10,\n",
       " 'evaluation_duration_unit': 'episodes',\n",
       " 'evaluation_interval': None,\n",
       " 'evaluation_num_episodes': -1,\n",
       " 'evaluation_num_workers': 0,\n",
       " 'evaluation_parallel_to_training': False,\n",
       " 'exploration_config': {'final_scale': 0.02,\n",
       "  'initial_scale': 1.0,\n",
       "  'ou_base_scale': 0.1,\n",
       "  'ou_sigma': 0.2,\n",
       "  'ou_theta': 0.15,\n",
       "  'random_timesteps': 1000,\n",
       "  'scale_timesteps': 10000,\n",
       "  'type': 'OrnsteinUhlenbeckNoise'},\n",
       " 'explore': True,\n",
       " 'extra_python_environs_for_driver': {},\n",
       " 'extra_python_environs_for_worker': {},\n",
       " 'fake_sampler': False,\n",
       " 'framework': 'tfe',\n",
       " 'gamma': 0.99,\n",
       " 'grad_clip': None,\n",
       " 'horizon': None,\n",
       " 'huber_threshold': 1.0,\n",
       " 'ignore_worker_failures': False,\n",
       " 'in_evaluation': False,\n",
       " 'input': 'sampler',\n",
       " 'input_config': {},\n",
       " 'input_evaluation': ['is', 'wis'],\n",
       " 'keep_per_episode_custom_metrics': False,\n",
       " 'l2_reg': 1e-06,\n",
       " 'learning_starts': 1500,\n",
       " 'local_tf_session_args': {'inter_op_parallelism_threads': 8,\n",
       "  'intra_op_parallelism_threads': 8},\n",
       " 'log_level': 'WARN',\n",
       " 'log_sys_usage': True,\n",
       " 'logger_config': None,\n",
       " 'lr': 0.0001,\n",
       " 'metrics_episode_collection_timeout_s': 180,\n",
       " 'metrics_num_episodes_for_smoothing': 100,\n",
       " 'metrics_smoothing_episodes': -1,\n",
       " 'min_iter_time_s': -1,\n",
       " 'min_sample_timesteps_per_reporting': None,\n",
       " 'min_time_s_per_reporting': 1,\n",
       " 'min_train_timesteps_per_reporting': None,\n",
       " 'model': {'_disable_action_flattening': False,\n",
       "  '_disable_preprocessor_api': False,\n",
       "  '_time_major': False,\n",
       "  '_use_default_native_models': False,\n",
       "  'attention_dim': 64,\n",
       "  'attention_head_dim': 32,\n",
       "  'attention_init_gru_gate_bias': 2.0,\n",
       "  'attention_memory_inference': 50,\n",
       "  'attention_memory_training': 50,\n",
       "  'attention_num_heads': 1,\n",
       "  'attention_num_transformer_units': 1,\n",
       "  'attention_position_wise_mlp_dim': 32,\n",
       "  'attention_use_n_prev_actions': 0,\n",
       "  'attention_use_n_prev_rewards': 0,\n",
       "  'conv_activation': 'relu',\n",
       "  'conv_filters': None,\n",
       "  'custom_action_dist': None,\n",
       "  'custom_model': None,\n",
       "  'custom_model_config': {},\n",
       "  'custom_preprocessor': None,\n",
       "  'dim': 84,\n",
       "  'fcnet_activation': 'tanh',\n",
       "  'fcnet_hiddens': [256, 256],\n",
       "  'framestack': True,\n",
       "  'free_log_std': False,\n",
       "  'grayscale': False,\n",
       "  'lstm_cell_size': 256,\n",
       "  'lstm_use_prev_action': False,\n",
       "  'lstm_use_prev_action_reward': -1,\n",
       "  'lstm_use_prev_reward': False,\n",
       "  'max_seq_len': 20,\n",
       "  'no_final_linear': False,\n",
       "  'post_fcnet_activation': 'relu',\n",
       "  'post_fcnet_hiddens': [],\n",
       "  'use_attention': False,\n",
       "  'use_lstm': False,\n",
       "  'vf_share_layers': True,\n",
       "  'zero_mean': True},\n",
       " 'monitor': -1,\n",
       " 'multiagent': {'count_steps_by': 'env_steps',\n",
       "  'observation_fn': None,\n",
       "  'policies': {},\n",
       "  'policies_to_train': None,\n",
       "  'policy_map_cache': None,\n",
       "  'policy_map_capacity': 100,\n",
       "  'policy_mapping_fn': None,\n",
       "  'replay_mode': 'independent'},\n",
       " 'n_step': 1,\n",
       " 'no_done_at_end': False,\n",
       " 'normalize_actions': True,\n",
       " 'num_cpus_for_driver': 1,\n",
       " 'num_cpus_per_worker': 1,\n",
       " 'num_envs_per_worker': 1,\n",
       " 'num_gpus': 0,\n",
       " 'num_gpus_per_worker': 0,\n",
       " 'num_workers': 1,\n",
       " 'observation_filter': 'NoFilter',\n",
       " 'observation_space': None,\n",
       " 'optimizer': {},\n",
       " 'output': None,\n",
       " 'output_compress_columns': ['obs', 'new_obs'],\n",
       " 'output_config': {},\n",
       " 'output_max_file_size': 67108864,\n",
       " 'placement_strategy': 'PACK',\n",
       " 'policy_delay': 1,\n",
       " 'postprocess_inputs': False,\n",
       " 'preprocessor_pref': 'deepmind',\n",
       " 'prioritized_replay': True,\n",
       " 'prioritized_replay_alpha': 0.6,\n",
       " 'prioritized_replay_beta': 0.4,\n",
       " 'prioritized_replay_eps': 1e-06,\n",
       " 'record_env': False,\n",
       " 'recreate_failed_workers': False,\n",
       " 'remote_env_batch_wait_ms': 0,\n",
       " 'remote_worker_envs': False,\n",
       " 'render_env': False,\n",
       " 'replay_buffer_config': {'capacity': 50000, 'type': 'MultiAgentReplayBuffer'},\n",
       " 'rollout_fragment_length': 1,\n",
       " 'sample_async': False,\n",
       " 'seed': None,\n",
       " 'shuffle_buffer_size': 0,\n",
       " 'simple_optimizer': -1,\n",
       " 'smooth_target_policy': False,\n",
       " 'soft_horizon': False,\n",
       " 'store_buffer_in_checkpoints': False,\n",
       " 'synchronize_filters': True,\n",
       " 'target_network_update_freq': 0,\n",
       " 'target_noise': 0.2,\n",
       " 'target_noise_clip': 0.5,\n",
       " 'tau': 0.002,\n",
       " 'tf_session_args': {'allow_soft_placement': True,\n",
       "  'device_count': {'CPU': 1},\n",
       "  'gpu_options': {'allow_growth': True},\n",
       "  'inter_op_parallelism_threads': 2,\n",
       "  'intra_op_parallelism_threads': 2,\n",
       "  'log_device_placement': False},\n",
       " 'timesteps_per_iteration': 1000,\n",
       " 'train_batch_size': 256,\n",
       " 'training_intensity': None,\n",
       " 'twin_q': False,\n",
       " 'use_huber': False,\n",
       " 'use_state_preprocessor': False,\n",
       " 'worker_side_prioritization': False}"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import yaml\n",
    "import os\n",
    "realpath=\"/home/sunshuo/qml/TradeMaster_reframe/\"\n",
    "dict=\"input_config/agent/SOTA/DDPG.yml\"\n",
    "yamlpath=os.path.join(realpath,dict)\n",
    "with open(yamlpath, \"r\", encoding=\"utf-8\") as f:\n",
    "    config=yaml.safe_load(f)\n",
    "config"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'df_dict': '/home/sunshuo/qml/TradeMaster_reframe/experiment_result/data/test.csv',\n",
       " 'initial_amount': 100000,\n",
       " 'tech_indicator_list': ['zopen',\n",
       "  'zhigh',\n",
       "  'zlow',\n",
       "  'zadjcp',\n",
       "  'zclose',\n",
       "  'zd_5',\n",
       "  'zd_10',\n",
       "  'zd_15',\n",
       "  'zd_20',\n",
       "  'zd_25',\n",
       "  'zd_30'],\n",
       " 'transaction_cost_pct': 0.0001}"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import yaml\n",
    "import os\n",
    "realpath=\"/home/sunshuo/qml/TradeMaster_reframe/\"\n",
    "dict=\"input_config/env/portfolio/test.yml\"\n",
    "yamlpath=os.path.join(realpath,dict)\n",
    "with open(yamlpath, \"r\", encoding=\"utf-8\") as f:\n",
    "    config=yaml.safe_load(f)\n",
    "config"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "/home/sunshuo/qml/TradeMaster_reframe/agent/SOTA\n",
      "0\n"
     ]
    }
   ],
   "source": [
    "print(os.system(\"realpath .\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/home/sunshuo/qml/TradeMaster_reframe/agent/SOTA\\n'"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "p=os.popen('realpath .')\n",
    "p.read()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "realpath=\"/home/sunshuo/qml/TradeMaster_reframe\"\n",
    "file_dict=os.path.join(realpath,dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-07-07 17:16:41,044\tINFO services.py:1470 -- View the Ray dashboard at \u001b[1m\u001b[32mhttp://127.0.0.1:8265\u001b[39m\u001b[22m\n"
     ]
    }
   ],
   "source": [
    "import ray\n",
    "ray.init()\n",
    "ray.shutdown()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "2022-07-07 17:25:06,169\tINFO services.py:1470 -- View the Ray dashboard at \u001b[1m\u001b[32mhttp://127.0.0.1:8265\u001b[39m\u001b[22m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "<function ray.worker.shutdown(_exiting_interpreter: bool = False)>"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import ray\n",
    "ray.init()\n",
    "ray.shutdown()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"/home/sunshuo/ray_results/A2CTrainer_portfolio_2022-07-08_00-13-16s4aa6of_/checkpoint_000001/checkpoint-1\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'/home/sunshuo/ray_results/A2CTrainer_portfolio_2022-07-08_00-13-16s4aa6of_/checkpoint_000001'"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import os\n",
    "os.path.split(\"/home/sunshuo/ray_results/A2CTrainer_portfolio_2022-07-08_00-13-16s4aa6of_/checkpoint_000001/checkpoint-1\")[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3.8.13 ('TradeMaster')",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.13"
  },
  "orig_nbformat": 4,
  "vscode": {
   "interpreter": {
    "hash": "a1f97403911abd3f02553c8f2b0c54537fddc7efadd9f5d3e31784db6e40c347"
   }
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
